package com.zbkj.front.config;

import cn.hutool.core.io.IoUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "pay.alipay")
public class AliConfig {

    /**
     * 应用ID
     */
    private String appId;
    /**
     * 应用私钥
     */
    private String appPrivateKey;
    /**
     * 支付宝公钥
     */
    private String alipayPublicKey;


    private String privateKeyPath;

    private String publicKeyPath;

    private String notifyUrl;

    // 支付成功返回地址
    private String returnUrl;



    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        appPrivateKey = IoUtil.read(this.getClass().getClassLoader().getResourceAsStream(privateKeyPath), "UTF8");
        alipayPublicKey = IoUtil.read(this.getClass().getClassLoader().getResourceAsStream(publicKeyPath), "UTF8");

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(appPrivateKey);
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setFormat("json");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return new DefaultAlipayClient(alipayConfig);
    }


}
